<?php
namespace app\model;

use think\Db;

class CourierOrder extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'courier_order';
    }

    /**
     * 类入口
     * @return CourierOrder
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 验证id
     * @param $int_question_id int 用户地址id
     * @return bool
     */
    public function checkOrderId($int_order_id)
    {
        $order_id = $this->toValue(['order_id' => $int_order_id], 'order_id');
        if ($order_id > 0) return true;
        return false;
    }
    /**
     * 获取快递员上下班状态
     *
     */
    public function getCourierDutyState($int_courier_id)
    {
        return $this->toValue(['id'=>$int_courier_id],'is_on_duty');
    }

    /**
     * 快递员管理订单列表总数
     */
    public function getToCount($arr_where,$sort){
        if ($sort==1){
            return $data = Db::name($this->db_name)
                ->where($arr_where)
                ->join('user_order','courier_order.order_id=user_order.order_id','left')
                ->count();
        }elseif ($sort==2){
            return $data = Db::name($this->db_name)
                ->where($arr_where)
                ->join('user_order','courier_order.order_id=user_order.order_id','left')
                ->join('courier_info','courier_info.id=courier_order.courier_id','left')
                ->count();
        }

    }

    /**
     * 快递员管理订单列表
     */
    public function getCourierOrderList($arr_where,$field,$int_first_row,$int_list_rows,$sort,$rank){
        if ($sort==1){
            $data =  Db::name($this->db_name)
                ->where($arr_where)
                ->field($field)
                ->join('user_order','courier_order.order_id=user_order.order_id','left')
                ->limit($int_first_row,$int_list_rows)
                ->order("courier_order.gmt_create $rank")
                ->select();
        }elseif ($sort==2){
            $data =  Db::name($this->db_name)
                ->where($arr_where)
                ->field($field)
                ->join('user_order','courier_order.order_id=user_order.order_id','left')
                ->join('courier_info','courier_info.id=courier_order.courier_id','left')
                ->limit($int_first_row,$int_list_rows)
                ->order("courier_order.gmt_create $rank")
                ->select();
        }


        foreach ($data as $key=>$val){
            $describe = Db::name('user_order_log')
                ->where("order_id={$val['orderId']}")
                ->order('gmt_create','desc')
                ->value('describe');
            $data[$key]['describe']=$describe;
        }
        return $data;
    }


    /**
     * 快递员信息
     */
    public function getCourierInfo($arr_where){
        $data = Db::name($this->db_name)
            ->where($arr_where)
            ->field('courier_info.id,courier_info.real_name AS realName,courier_info.mobile,courier_order.pick_code AS pickCode,courier_info.express,courier_info.is_official AS isOfficial,courier_order.road_map AS roadMap,courier_order.take_road_map AS takeRoadMap')
            ->join('courier_info','courier_order.courier_id=courier_info.id','left')
            ->find();
        return $data;
    }
}
